package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class PersonCon here.
 * 
 * @author (Frederik, Simon & Claus) 
 * @version (a version number or a date)
 */
public class DvdCon
{
    // instance variables - replace the example below with your own
    private ArrayList<Dvd> dvds;
    private static DvdCon instance;
    private Dvd d;

    /**
     * Constructor for objects of class PersonCon
     */
    private DvdCon()
    {
        // initialise instance variables

        dvds = new ArrayList<Dvd>();
    }  

    public static DvdCon getInstance()
    {
        if(instance == null)
        {
            instance = new DvdCon();
        }
        return instance;
    }

    public void addDvd(Dvd d)
    {
        dvds.add(d);
    }

    public void deleteDvd(Dvd d)
    {
        dvds.remove(d);
    }
    
    public void deleteCopy(String serialNumber)
    {
        int i = 0;
        boolean found =false;
        Copy c = null;
        while (!found && i<dvds.size()) {
            if (dvds.get(i).findCopy(serialNumber) == null) {
                i++;
            }
            else if (serialNumber.equals (dvds.get(i).findCopy(serialNumber).getSerialNumber())) {
                found = true;
                c = dvds.get(i).findCopy(serialNumber);
            }
            else {
                i++;
            }
            if (found) {
                dvds.get(i).deleteCopy(c);
            }
        }
    }

    public Dvd findDvd(String title)
    {
        int i = 0;
        boolean found = false;
        while (!found && i<dvds.size()) {

            if (title.equals (dvds.get(i).getTitle())) {
                found = true;
            }
            else {
                i++;
            }

            if (found) {                
                return dvds.get(i);
            }

        }
        return null;
    }

    public Copy findCopy(String serialNumber)
    {

        int i = 0;
        boolean found =false;
        while (!found && i<dvds.size()) {
            if (dvds.get(i).findCopy(serialNumber) == null) {
                i++;
            }
            else if (serialNumber.equals (dvds.get(i).findCopy(serialNumber).getSerialNumber())) {
                found = true;
            }
            else {
                i++;
            }
            if (found) {
                return dvds.get(i).findCopy(serialNumber);
            }
        }
        return null;
    }

    public int getDvdsSize()
    {
        return dvds.size();
    }

    public void clearDvds()
    {
        dvds.clear();
    }

}
